//! `std::io` 模块的测试用例
//!
//! 本文件包含对 `std::io` 中 `Read`, `Write`, `Seek` 等 trait 的测试。
//! 测试主要使用内存中的缓冲区 (`Cursor`) 和临时文件来模拟 IO 操作。

#[cfg(test)]
mod tests {
    use std::fs::{self, File};
    use std::io::{self, BufRead, BufReader, Cursor, Read, Seek, SeekFrom, Write};

    // --- Read Trait 测试 ---

    /// 测试从 `Cursor` (内存缓冲区) 读取数据
    #[test]
    fn test_read_from_cursor() {
        let data = b"some bytes";
        let mut cursor = Cursor::new(data);
        let mut buffer = [0; 4];

        // 读取前 4 个字节
        let num_bytes = cursor.read(&mut buffer).unwrap();
        assert_eq!(num_bytes, 4);
        assert_eq!(&buffer, b"some");
    }

    /// 测试 `read_to_string`
    #[test]
    fn test_read_to_string() {
        let data = b"hello world";
        let mut cursor = Cursor::new(data);
        let mut string = String::new();

        cursor.read_to_string(&mut string).unwrap();
        assert_eq!(string, "hello world");
    }

    // --- Write Trait 测试 ---

    /// 测试向 `Vec<u8>` 写入数据
    #[test]
    fn test_write_to_vec() {
        let mut buffer: Vec<u8> = Vec::new();
        buffer.write_all(b"first part").unwrap();
        buffer.write_all(b" and second part").unwrap();

        assert_eq!(buffer, b"first part and second part");
    }

    /// 测试 `write!` 宏
    #[test]
    fn test_write_macro() {
        let mut buffer: Vec<u8> = Vec::new();
        write!(&mut buffer, "Formatted: {}, {}", 123, "abc").unwrap();

        assert_eq!(buffer, b"Formatted: 123, abc");
    }

    // --- Seek Trait 测试 ---

    /// 测试在 `Cursor` 中移动读写指针
    #[test]
    fn test_seek_in_cursor() {
        let mut cursor = Cursor::new(vec![0, 1, 2, 3, 4, 5]);

        // 从头开始移动 3 个字节
        cursor.seek(SeekFrom::Start(3)).unwrap();
        let mut byte = [0];
        cursor.read_exact(&mut byte).unwrap();
        assert_eq!(byte[0], 3);

        // 从当前位置向后移动 1 个字节
        cursor.seek(SeekFrom::Current(1)).unwrap();
        cursor.read_exact(&mut byte).unwrap();
        assert_eq!(byte[0], 5);

        // 从末尾向前移动 2 个字节
        cursor.seek(SeekFrom::End(-2)).unwrap();
        cursor.read_exact(&mut byte).unwrap();
        assert_eq!(byte[0], 4);
    }

    // --- BufReader 和 BufWriter 测试 ---

    /// 测试 `BufReader` 逐行读取
    #[test]
    fn test_bufreader_lines() {
        let data = b"line 1\nline 2\nline 3";
        let reader = BufReader::new(Cursor::new(data));

        let lines: Vec<_> = reader.lines().map(|l| l.unwrap()).collect();
        assert_eq!(lines, vec!["line 1", "line 2", "line 3"]);
    }

    /// 测试 `BufWriter` 缓冲写入
    #[test]
    fn test_bufwriter() {
        let test_dir = "test_io_temp";
        fs::create_dir_all(test_dir).unwrap();
        let file_path = format!("{}/bufwriter_test.txt", test_dir);

        let file = File::create(&file_path).unwrap();
        let mut writer = io::BufWriter::new(file);

        writer.write_all(b"Buffered write.").unwrap();
        // 此时数据可能还在缓冲区，没有完全写入文件

        // `flush` 会确保所有缓冲数据都写入底层 writer
        writer.flush().unwrap();

        let content = fs::read_to_string(&file_path).unwrap();
        assert_eq!(content, "Buffered write.");

        fs::remove_dir_all(test_dir).unwrap();
    }

    // --- io::copy ---

    /// 测试 `io::copy`
    #[test]
    fn test_io_copy() {
        let mut source = Cursor::new(b"data to be copied");
        let mut sink = Vec::new();

        let bytes_copied = io::copy(&mut source, &mut sink).unwrap();

        assert_eq!(bytes_copied, 17);
        assert_eq!(sink, b"data to be copied");
    }
}
